package com.healthcarex.hip.saas.web.service.impl;

import com.healthcarex.hip.saas.common.model.SysWebLog;
import com.healthcarex.hip.saas.mybatis.service.impl.BaseServiceImpl;
import com.healthcarex.hip.saas.web.dao.WebLogDao;
import com.healthcarex.hip.saas.web.service.IWebLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.Arrays;
import java.util.List;

/**
 * @author chenpan
 * @ClassName: WebLogServiceImpl
 * description:
 * @date 2018-04-02 16:08
 */
@Service
public class WebLogServiceImpl extends BaseServiceImpl<SysWebLog> implements IWebLogService {

    @Autowired
    private WebLogDao webLogDao;

    @Override
    public List<SysWebLog> findAllWebLogs(SysWebLog log) {
        Example example = new Example(SysWebLog.class);
        Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(log.getUsername())) {
            criteria.andCondition("username=", log.getUsername());
        }
        if (StringUtils.isNotBlank(log.getOperation())) {
            criteria.andCondition("operation like", "%" + log.getOperation() + "%");
        }
        example.setOrderByClause("create_time");
        return this.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, readOnly = false)
    public void deleteWebLogs(String logIds) {
        List<String> list = Arrays.asList(logIds.split(","));
        this.batchDelete(list, "id", SysWebLog.class);
    }

    @Override
    public void deleteAllLogs() {
        webLogDao.truncateLogs();
    }
}
